package android.sdk;

import com.acelerometro.R;

import android.app.Activity;
import android.content.Context;
import android.dto.AccelerometerEvent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Ejemplo extends Activity implements SensorEventListener {

    TextView x;
    TextView y; 
    TextView z; 
    TextView status;
    long lastTimeStamp = 0L;
    
	private NetworkController network;
	
    SensorManager sensorManager; 
    PowerManager.WakeLock wakeLock;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        
        PowerManager powerManager= (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Tesis");
        wakeLock.acquire();
        
        network = new NetworkController(new SDKClientActivity());
        //network.connect();
        
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
        setContentView(R.layout.main);
        
	    this.x = (TextView)this.findViewById(R.id.x);
	    this.y = (TextView)this.findViewById(R.id.y);
	    this.z = (TextView)this.findViewById(R.id.z);
	    
	    this.status = (TextView)this.findViewById(R.id.status);
        
        this.findViewById(R.id.Button01).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Toast.makeText(Ejemplo.this,"Botón 1",Toast.LENGTH_SHORT).show();
			}});
        this.findViewById(R.id.Button02).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Toast.makeText(Ejemplo.this,"Botón 2",Toast.LENGTH_SHORT).show();		
			}});
    }

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		
	}
	
	@Override
	public void onSensorChanged(SensorEvent event) {
		if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			this.x.setText(String.format("%.2f", event.values[0]));
			this.y.setText(String.format("%.2f", event.values[1]));
			this.z.setText(String.format("%.2f", event.values[2]));
			
			long timeStamp = event.timestamp;
			this.status.setText(String.valueOf((timeStamp - lastTimeStamp)/1000000));
			lastTimeStamp = timeStamp;
			
			AccelerometerEvent m = new AccelerometerEvent();
			m.setX(event.values[0]);
			m.setY(event.values[1]);
			m.setZ(event.values[2]);
			
			network.sendObject(m);
			
		}
	}

	@Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
        wakeLock.acquire();
	}

    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
        wakeLock.release();
    }
    
    protected void onDestroy() {
        super.onDestroy();
        Log.i("Tesis", "onDestroy()");
        sensorManager.unregisterListener(this);
        wakeLock.release();
        network.close();
    }
    
}